package com.wandian.weather.tools;



import com.wandian.weather.bean.ShenshaBean;
import com.wandian.weather.bean.YijiBean;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

/**
 * 旧历天干地支工具类
 */

public class LunarCalendar {

    // 获取天干地支数据
    public HashMap<String, Object> getBaseInfo(int y, int m, int d) {
        return this.calendarint(y, m, d);
    }

    // 获取天干地支数据
    public HashMap<String, Object> calendarint(int y, int m, int d) {
        HashMap<String, Object> map = new HashMap<>();

        int lM2 = (y - 1900) * 12 + m + 12;
        Calendar calendar = Calendar.getInstance();
        calendar.set(y, (m - 1), d);
        Long date2 = calendar.getTimeInMillis();
        Long daycyclical = date2 / 86400000 + 25567 + 10;
        //计算每日胎神方位
        HashMap<String, String> taishenMap = getcyclicaltaishen(daycyclical);
        map.put("childofGod", taishenMap);
        Long lD2 = daycyclical;
        int bsg = (int) (lD2 % 12);
        int cs1 = d + 1;

        // 计算 命进禄  ???
        String dayglus = "";
        switch (bsg) {
            case 0:
                dayglus = LunarCalendarData.Gan[9] + "命进禄 ";
                break;
            case 2:
                dayglus = LunarCalendarData.Gan[0] + "命进禄 ";
                break;
            case 3:
                dayglus = LunarCalendarData.Gan[1] + "命进禄 ";
                break;
            case 5:
                dayglus = LunarCalendarData.Gan[2] + "命进禄 ";
                break;
            case 6:
                dayglus = LunarCalendarData.Gan[3] + "命进禄 ";
                break;
            case 8:
                dayglus = LunarCalendarData.Gan[6] + "命进禄 ";
                break;
            case 9:
                dayglus = LunarCalendarData.Gan[7] + "命进禄 ";
                break;
            case 11:
                dayglus = LunarCalendarData.Gan[8] + "命进禄 ";
                break;
        }
        // 计算喜神方位
        String xs = "";
        switch ((int) (lD2 % 10)) {
            case 0:
            case 5:
                xs = "东北";
                break;
            case 1:
            case 6:
                xs = "西北";
                break;
            case 2:
            case 7:
                xs = "西南";
                break;
            case 3:
            case 8:
                xs = "正南";
                break;
            case 4:
            case 9:
                xs = "东南";
                break;
        }
        //计算福神方位
        String fs = "";
        if ((lD2) % 10 == 0 || (lD2) % 10 == 1) {
            fs = "东南";
        } else if ((lD2) % 10 == 2 || (lD2) % 10 == 3) {
            fs = "正东";
        } else if ((lD2) % 10 == 4) {
            fs = "正北";
        } else if ((lD2) % 10 == 5) {
            fs = "正南";
        } else if ((lD2) % 10 == 6 || (lD2) % 10 == 7) {
            fs = "西南";
        } else if ((lD2) % 10 == 8) {
            fs = "西北";
        } else if ((lD2) % 10 == 9) {
            fs = "正西";
        }

        //计算财神方位
        String cs = "";
        if ((lD2) % 10 == 0 || (lD2) % 10 == 1) {
            cs = "东北";
        } else if ((lD2) % 10 == 2 || (lD2) % 10 == 3) {
            cs = "西南";
        } else if ((lD2) % 10 == 4 || (lD2) % 10 == 5) {
            cs = "正北";
        } else if ((lD2) % 10 == 6 || (lD2) % 10 == 7) {
            cs = "正东";
        } else if ((lD2) % 10 == 8 || (lD2) % 10 == 9) {
            cs = "正南";
        }
        //彭祖
        HashMap<String, Object> pgday = cyclical3(lD2);
        map.put("pengzhu", pgday);

        //时辰
        String dGz = cyclical4(lD2);
        String sgz = cyclical8(lD2);
        String shic[] = dGz.split(" ");
        String xj[] = sgz.split(" ");
        List<HashMap<String, String>> shiichen = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("shichen", shic[i]);
            hashMap.put("xiongji", xj[i]);
            shiichen.add(hashMap);
        }
        map.put("shichen", shiichen);

        // wuxing
        HashMap<String, String> sgz2 = jzny(lD2 % 10 + "" + lD2 % 12);
        map.put("wuxing", sgz2);

        // chongsha
        HashMap<String, Object> sgz4 = CalConv((int) (lD2 % 10), (int) (lD2 % 12));
        map.put("chongsha", sgz4);

        //zhisheng
        String sgz6 = cyclical7((int) (lM2 % 12), (int) (lD2 % 12));
        String modern_zhishen = LunarCalendarData.Zhishenjieshi.get(sgz6);
        HashMap<String, String> zhishen =  new HashMap<>();
        zhishen.put("zhishen",sgz6);
        zhishen.put("modern_zhishen",modern_zhishen);
        map.put("zhishen",zhishen);

        // jianchu
        String sgz3 = cyclical6((int) (lM2 % 12), (int) (lD2 % 12));
        String modern_jianchu = LunarCalendarData.Jianchu.get(sgz3);
        HashMap<String, String> jianchu =  new HashMap<>();
        jianchu.put("jianchuri",sgz3);
        jianchu.put("modern_jianchuri",modern_jianchu);
        map.put("jianchuri",jianchu);

        // minglu
        String dayglu = LunarCalendarData.Dayglk[(int) (lD2 % 10)] + "命互禄" + dayglus;

        map.put("xishen", xs);
        map.put("fushen", fs);
        map.put("caishen", cs);
        map.put("minglu", dayglu);

        // xingxiu
        HashMap<String, String> xingxiu = GetSukuD((int) Ymd2Jd(y, m, d));
        map.put("xingxiu", xingxiu);

        // yiji
        String sgzday = cyclical(lD2);
        HashMap<String, Object> yiji = getdayyiji(sgzday, (int) (lD2 % 12));
        map.put("yiji", yiji);

        // shensha
        HashMap<String, String> shensha = getshensha(y, m, d);
        map.put("shensha",shensha);

        HashMap<String, String> oldtime =  new HashMap<>();
        oldtime.put("year",""+y);
        oldtime.put("month", cyclical((long) ((y - 1900) * 12 + m + 12)));
        oldtime.put("day", sgzday);
        map.put("oldtime",oldtime);

        return map;
    }

    /**
     * 计算时间天的每个时辰的凶吉，神位，宜忌
     *
     * @return array
     */
    public List<HashMap<String, String>> info(int y, int m, int day) {
        List<HashMap<String, String>> list = new ArrayList<>();

        int lM2 = (y - 1900) * 12 + m + 12;
        Calendar calendar = Calendar.getInstance();
        calendar.set(y, (m - 1), day);
        Long date2 = calendar.getTimeInMillis();
        Long daycyclical = date2 / 86400000 + 25567 + 10;
        //日干
        int gan = (int) (daycyclical % 10);
        int zhi = (int) (daycyclical % 12);

        String xiongji[] = LunarCalendarData.Gan6[zhi].split(" ");
        String dGz[] = LunarCalendarData.Gan3[gan].split(" ");


        for (int i = 1; i <= 12; i++) {
            HashMap<String, String> jcrjx = new HashMap<>();
            //计算时干
            int dd = gan % 10 + i - 1;
            if (LunarCalendarData.AnimalIdx2[i - 1] == LunarCalendarData.AnimalIdx2[(int) (daycyclical % 12)]) {
//                jcrjx =array('yi'=>'无','ji'=>'诸事不宜');
                jcrjx.put("yi", "无");
                jcrjx.put("ji", "诸事不宜");
            } else {
                String d = LunarCalendarData.JcName0[dd % 10];
                if (d.equals("建")) {
                    jcrjx.put("yi", "出行 上任 会友 上书 见工");
                    jcrjx.put("ji", "动土 开仓 嫁娶 纳采");
                }
                if (d.equals("除")) {
                    jcrjx.put("yi", "除服 疗病 出行 拆卸 入宅");
                    jcrjx.put("ji", "求官 上任 开张 搬家 探病");
                }
                if (d.equals("满")) {
                    jcrjx.put("yi", "祈福 祭祀 结亲 开市 交易");
                    jcrjx.put("ji", "服药 求医 栽种 动土 迁移");
                }
                if (d.equals("平")) {
                    jcrjx.put("yi", "祭祀 修墳 涂泥 餘事勿取");
                    jcrjx.put("ji", "移徙.入宅.嫁娶.开市.安葬");
                }
                if (d.equals("定")) {
                    jcrjx.put("yi", "交易 立券 会友 签約 納畜");
                    jcrjx.put("ji", "种植 置业 卖田 掘井 造船");
                }
                if (d.equals("执")) {
                    jcrjx.put("yi", "祈福 祭祀 求子 结婚 立约");
                    jcrjx.put("ji", "开市 交易 搬家 远行");
                }
                if (d.equals("破")) {
                    jcrjx.put("yi", "求医 赴考 祭祀 餘事勿取");
                    jcrjx.put("ji", "动土 出行 移徙 开市 修造");
                }
                if (d.equals("危")) {
                    jcrjx.put("yi", "经营 交易 求官 納畜 動土");
                    jcrjx.put("ji", "登高 行船.安床.入宅.博彩");
                }
                if (d.equals("成")) {
                    jcrjx.put("yi", "祈福 入学 开市 求医 成服");
                    jcrjx.put("ji", "词讼 安門 移徙");
                }
                if (d.equals("收")) {
                    jcrjx.put("yi", "祭祀 求财 签约 嫁娶 订盟");
                    jcrjx.put("ji", "开市.安床.安葬.入宅.破土");
                }
                if (d.equals("开")) {
                    jcrjx.put("yi", "疗病 结婚 交易 入仓 求职");
                    jcrjx.put("ji", "安葬 动土 针灸");
                }
                if (d.equals("闭")) {
                    jcrjx.put("yi", "祭祀 交易 收财 安葬");
                    jcrjx.put("ji", "宴会 安床 出行 嫁娶 移徙");
                }
            }
            jcrjx.put("xiongji", xiongji[i - 1]);
            jcrjx.put("caishen", getshigan(dGz[i - 1]));
            jcrjx.put("xishen", shichen(dd));
            jcrjx.put("fushen", fushen(dd));
            jcrjx.put("shichen", dGz[i - 1]);
            jcrjx.put("chong", LunarCalendarData.AnimalIdx2[i - 1]);
            jcrjx.put("sha", LunarCalendarData.Sfw[i - 1]);
            list.add(jcrjx);
        }
        return list;
    }

    /**
     * 获取天干地支
     */
    public String cyclical(Long num) {
        return LunarCalendarData.Gan[(int) (num % 10)] + "" + LunarCalendarData.Zhi[(int) (num % 12)];
    }

    /**
     * 获取彭祖
     */
    public HashMap<String, Object> cyclical3(Long num) {
        HashMap<String, Object> map = new HashMap<>();
        map.put(LunarCalendarData.PENGZHU, LunarCalendarData.Gan2[(int) (num % 10)] + " " + LunarCalendarData.Zhi2[(int) (num % 12)]);

        List<HashMap<String, String>> list = new ArrayList<>();
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("title", LunarCalendarData.Pengzhu.get(LunarCalendarData.Gan2[(int) (num % 10)]));

        HashMap<String, String> map2 = new HashMap<>();
        map2.put("title", LunarCalendarData.Pengzhu.get(LunarCalendarData.Zhi2[(int) (num % 12)]));

        list.add(map1);
        list.add(map2);
        map.put(LunarCalendarData.PENGZHU_MODERN, list);


        return map;
    }

    /**
     * 获取时辰
     *
     * @param num
     * @return
     */
    public String cyclical4(Long num) {
        return (LunarCalendarData.Gan3[(int) (num % 10)]);
    }

    // 获取建除
    public String cyclical6(int num, int num2) {
        if (num == 0) return (LunarCalendarData.JcName0[num2]);
        if (num == 1) return (LunarCalendarData.JcName1[num2]);
        if (num == 2) return (LunarCalendarData.JcName2[num2]);
        if (num == 3) return (LunarCalendarData.JcName3[num2]);
        if (num == 4) return (LunarCalendarData.JcName4[num2]);
        if (num == 5) return (LunarCalendarData.JcName5[num2]);
        if (num == 6) return (LunarCalendarData.JcName6[num2]);
        if (num == 7) return (LunarCalendarData.JcName7[num2]);
        if (num == 8) return (LunarCalendarData.JcName8[num2]);
        if (num == 9) return (LunarCalendarData.JcName9[num2]);
        if (num == 10) return (LunarCalendarData.JcName10[num2]);
        if (num == 11) return (LunarCalendarData.JcName11[num2]);
        return "";
    }

    // 获取 值神
    public String cyclical7(int num, int num2) {

        if (num == 2)
            return (LunarCalendarData.ZrxName1[num2]);
        if (num == 3)
            return (LunarCalendarData.ZrxName2[num2]);
        if (num == 4)
            return (LunarCalendarData.ZrxName3[num2]);
        if (num == 5)
            return (LunarCalendarData.ZrxName4[num2]);
        if (num == 6)
            return (LunarCalendarData.ZrxName5[num2]);
        if (num == 7)
            return (LunarCalendarData.ZrxName6[num2]);
        if (num == 8)
            return (LunarCalendarData.ZrxName7[num2]);
        if (num == 9)
            return (LunarCalendarData.ZrxName8[num2]);
        if (num == 10)
            return (LunarCalendarData.ZrxName9[num2]);
        if (num == 11)
            return (LunarCalendarData.ZrxName10[num2]);
        if (num == 0)
            return (LunarCalendarData.ZrxName11[num2]);
        if (num == 1)
            return (LunarCalendarData.ZrxName12[num2]);
        return "";
    }

    ///* 获取时辰
    public String cyclical8(Long num) {
        return (LunarCalendarData.Gan6[(int) (num % 12)]);
    }

    /**
     * 胎神
     *
     * @param daycyclical
     * @return
     */
    public HashMap<String, String> getcyclicaltaishen(Long daycyclical) {
        HashMap<String, String> taishen = new HashMap<>();
        String cD = cyclical(daycyclical);
        taishen.put(LunarCalendarData.TAISHEN, LunarCalendarData.Taishen.get(cD));
        taishen.put(LunarCalendarData.TAISHEN_MODERN, LunarCalendarData.Taishen_modern.get(LunarCalendarData.Taishen.get(cD)));
        return taishen;
    }

    // ???????????
    public void sTerm() {
    }

    // 获取五行
    public HashMap<String, String> jzny(String num) {
        HashMap<String, String> map = new HashMap<>();
        String ny = "";
        if (num.equals("00") || num.equals("11")) ny = "海中金";
        if (num.equals("22") || num.equals("33")) ny = "炉中火";
        if (num.equals("44") || num.equals("55")) ny = "大林木";
        if (num.equals("66") || num.equals("77")) ny = "路旁土";
        if (num.equals("88") || num.equals("99")) ny = "剑锋金";
        if (num.equals("010") || num.equals("111")) ny = "山头火";
        if (num.equals("20") || num.equals("31")) ny = "涧下水";
        if (num.equals("42") || num.equals("53")) ny = "城头土";
        if (num.equals("64") || num.equals("75")) ny = "白腊金";
        if (num.equals("86") || num.equals("97")) ny = "杨柳木";
        if (num.equals("08") || num.equals("19")) ny = "井泉水";
        if (num.equals("210") || num.equals("311")) ny = "屋上土";
        if (num.equals("40") || num.equals("51")) ny = "霹雷火";
        if (num.equals("62") || num.equals("73")) ny = "松柏木";
        if (num.equals("84") || num.equals("95")) ny = "常流水";
        if (num.equals("06") || num.equals("17")) ny = "沙中金";
        if (num.equals("28") || num.equals("39")) ny = "山下火";
        if (num.equals("410") || num.equals("511")) ny = "平地木";
        if (num.equals("60") || num.equals("71")) ny = "壁上土";
        if (num.equals("82") || num.equals("93")) ny = "金箔金";
        if (num.equals("04") || num.equals("15")) ny = "佛灯火";
        if (num.equals("26") || num.equals("37")) ny = "天河水";
        if (num.equals("48") || num.equals("59")) ny = "大驿土";
        if (num.equals("610") || num.equals("711")) ny = "钗钏金";
        if (num.equals("80") || num.equals("91")) ny = "桑柘木";
        if (num.equals("02") || num.equals("13")) ny = "大溪水";
        if (num.equals("24") || num.equals("35")) ny = "沙中土";
        if (num.equals("46") || num.equals("57")) ny = "天上火";
        if (num.equals("68") || num.equals("79")) ny = "石榴木";
        if (num.equals("810") || num.equals("911")) ny = "大海水";
        map.put("wx", ny);
        map.put(LunarCalendarData.WUXING_MODERN, LunarCalendarData.Wuxing.get(ny));
        return map;
    }

    /*    */

    /**
     * 时辰冲煞
     *
     * @return array
     *//*
    public String CalConvbyshichen(int hour) {
        String data = "";
        return data;
    }*/

    // 获取冲杀
    public HashMap<String, Object> CalConv(int d, int dd) {

        HashMap<String, Object> map = new HashMap<>();
        map.put(LunarCalendarData.CHONGSHA, "冲" + LunarCalendarData.AnimalIdx2[dd] + "煞" + LunarCalendarData.Sfw[dd]);

        List<HashMap<String, String>> list = new ArrayList<>();
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("title", "本日对属" + LunarCalendarData.AnimalIdx2[dd] + "的人不太有利。");

        HashMap<String, String> map2 = new HashMap<>();
        map2.put("title", "本日煞神方位在" + LunarCalendarData.Sfw[dd] + "方,向" + LunarCalendarData.Sfw[dd] + "方行事要小心。");

        list.add(map1);
        list.add(map2);
        map.put(LunarCalendarData.CHONGSHA_MODERN, list);

        return map;
    }

/*    // ???????????
    public void jznyy() {
    }*/

    /**
     * 计算星宿
     */
    public HashMap<String, String> GetSukuD(int dd) {
        HashMap<String, String> map = new HashMap<>();

        int s = (dd + 12) % 28;
        map.put(LunarCalendarData.XINGXIU, LunarCalendarData.Sukuyou[s] + LunarCalendarData.Sukuyou2[s]);
        map.put(LunarCalendarData.XINGXIU_MODERN, LunarCalendarData.Xingxiu.get(LunarCalendarData.Sukuyou9[s]));

        return map;
    }

    // ???????????
    public float Ymd2Jd(int yy, int mm, int dd) {
        int yym1 = yy - 1;
        float days = 1721422;
        LunarCalendarData.SolarMonth[1] = 28;

        if (yy % 4 == 0) {
            LunarCalendarData.SolarMonth[1] = 29;
            if (yy > 1582) {
                if (yy % 100 == 0) {
                    LunarCalendarData.SolarMonth[1] = 28;
                    if (yy % 400 == 0) {
                        LunarCalendarData.SolarMonth[1] = 29;
                    }
                }
            }
        }
        days += 365.25 * yym1 + 0.1;
        for (int m = 0; m < (mm - 1); m++) {
            days += LunarCalendarData.SolarMonth[m];
        }
        days += dd;
        if (days >= 2299160) days -= 10;
        if (yym1 >= 1600) {
            days -= (yym1 - 1600 + 0.1) / 100;
            days += (yym1 - 1600 + 0.1) / 400;
        }

        return days;
    }

    /**
     * 计算每个时辰财神的方位
     *
     * @return mixed
     */
    public String getshigan(String num) {
        return LunarCalendarData.Shichencaishen.get(num);
    }

    /**
     * 计算当前时辰的喜神方位
     *
     * @return mixed
     */
    public String shichen(int num) {
        return LunarCalendarData.Xishen[num % 10];
    }

    /**
     * 计算当前时辰的福神方位
     *
     * @return mixed
     */
    public String fushen(int num) {
        return LunarCalendarData.Fushen[num % 10];
    }

    /**
     * 计算当天的宜忌
     */
    public HashMap<String, Object> getdayyiji(String sgz, int jx) {
        try {
            HashMap<String, Object> map = new HashMap<>();
            String gz = LunarCalendarData.Sixshichen.get(sgz);

//        YijiBeanDao yijiDao = BGApplication.getInstance().getDaoSession().getYijiBeanDao();
//        QueryBuilder<YijiBean> qb = yijiDao.queryBuilder();
//        qb.where(YijiBeanDao.Properties.Gz.eq(gz), YijiBeanDao.Properties.Jx.eq(jx));

            List<YijiBean> list = DataSupport.where("gz=? and jx=?",gz, String.valueOf(jx)).find(YijiBean.class);

            if (list == null || list.size() == 0) {
                return map;
            }

            String yi = list.get(0).getYi();
            String ji = list.get(0).getJi();
            map.put("yi", yi);
            map.put("ji", ji);

            String yiArray[] = yi.split(" ");
            String jiArray[] = ji.split(" ");

            List<HashMap<String, String>> list1 = new ArrayList<>();
            for (String str : yiArray) {
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("title", str);
                hashMap.put("describe", LunarCalendarData.Jieshi.get(str));
                list1.add(hashMap);
            }
            List<HashMap<String, String>> list2 = new ArrayList<>();
            for (String str : jiArray) {
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("title", str);
                hashMap.put("describe", LunarCalendarData.Jieshi.get(str));
                list2.add(hashMap);
            }
            map.put("modern_yi", list1);
            map.put("modern_ji", list2);
            return map;
        }catch (Exception e){
            return  new HashMap<>();
        }
    }

    /**
     * 查询采集数据当天的神煞
     *
     * @return array
     */
    public HashMap<String, String> getshensha(int y, int m, int d) {

        HashMap<String, String> hashMap = new HashMap<>();

        String datetime = "d" + ((("" + m).length() < 2) ? "0" +m : m) +""+ ((("" + d).length() < 2) ? "0" +d : d) ;
//        ShenshaBeanDao shenshaDao = BGApplication.getInstance().getDaoSession().getShenshaBeanDao();
//        QueryBuilder<ShenshaBean> qb = shenshaDao.queryBuilder();
//        qb.where(ShenshaBeanDao.Properties.Year.eq(y), ShenshaBeanDao.Properties.Datetime.eq(datetime));

        List<ShenshaBean> list = DataSupport.where("year=? and datetime=?", String.valueOf(y),datetime).find(ShenshaBean.class);
        if (list == null || list.size() == 0) {
            return hashMap;
        }
        hashMap.put("jishen",list.get(0).getYq());
        hashMap.put("xiongshen",list.get(0).getXs());

        return hashMap;
    }
}
